package com.gitee.ywj1352.sharing;

import com.gitee.ywj1352.sharing.db.DataSourceContextHolder;
import com.gitee.ywj1352.sharing.mapper.OrdersDao;
import com.gitee.ywj1352.sharing.mapper.UserDao;
import com.gitee.ywj1352.sharing.po.Orders;
import com.gitee.ywj1352.sharing.po.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import java.math.BigDecimal;
import java.util.Date;

@SpringBootApplication(scanBasePackages = "com.gitee.ywj1352.sharing")
@MapperScan(value="com.gitee.ywj1352.sharing.mapper")
public class SharingApp {

    public static void main(String[] args) throws Exception{
        ConfigurableApplicationContext context = SpringApplication.run(SharingApp.class, args);
        testSharing(context);
    }


    public static void testSharing(ApplicationContext context){
        UserDao userDao = context.getBean(UserDao.class);
        OrdersDao ordersDao = context.getBean(OrdersDao.class);
        for (int i = 0; i < 100; i++) {
            User newUser = new User();
            newUser.setUserId(1251L+i);
            newUser.setAge(23);
            newUser.setCreateTime(new Date());
            newUser.setGender(true);
            newUser.setPassword("2344"+i);
            newUser.setPhone("18856173660");
            newUser.setUpdateTime(new Date());
            newUser.setRole(1);
            newUser.setUserName("test-ywj-02"+i);
            Orders orders = new Orders();
            orders.setUserId(newUser.getUserId());
            orders.setStatus(1);
            orders.setOrderId(101L + i);
            orders.setShoppingId(19L+i);
            orders.setSendTime(new Date());
            orders.setPostage(12);
            orders.setEndTime(new Date());
            orders.setPayment(new BigDecimal("123.12" + i));
            orders.setPaymentType(1);
            orders.setCreateTime(new Date());
            orders.setCloseTime(new Date());
            orders.setPaymentTime(new Date());
            userDao.insert(newUser);
            ordersDao.insert(orders);
        }
    }

    public static void testCreateQuery(ApplicationContext context){
        UserDao bean = context.getBean(UserDao.class);
        User newUser = new User();
        newUser.setUserId(125L);
        newUser.setAge(23);
        newUser.setCreateTime(new Date());
        newUser.setGender(true);
        newUser.setPassword("2344");
        newUser.setPhone("18856173660");
        newUser.setUpdateTime(new Date());
        newUser.setRole(1);
        newUser.setUserName("test-ywj-02");
        bean.insert(newUser);
        User user = bean.selectByPrimaryKey(1L);
        System.out.println(user);
    }

    public static void testDynamicDataSourceCreateQuery(ApplicationContext context){
        UserDao bean = context.getBean(UserDao.class);
        DataSourceContextHolder.setDataSource("master");
        User newUser = new User();
        newUser.setUserId(125L);
        newUser.setAge(23);
        newUser.setCreateTime(new Date());
        newUser.setGender(true);
        newUser.setPassword("2344");
        newUser.setPhone("18856173660");
        newUser.setUpdateTime(new Date());
        newUser.setRole(1);
        newUser.setUserName("test-ywj-02");
        bean.insert(newUser);
        DataSourceContextHolder.setDataSource("slave");
        User user = bean.selectByPrimaryKey(1L);
        System.out.println(user);
    }

}
